<html>
<head><meta charset="utf-8"><title>build-std + libc · t-cargo · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/index.html">t-cargo</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/build-std.20.2B.20libc.html">build-std + libc</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234260077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/build-std%20%2B%20libc/near/234260077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ender <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/build-std.20.2B.20libc.html#234260077">(Apr 13 2021 at 02:44)</a>:</h4>
<p>When you invoke build-std for a custom target, what does that target's libc consist of? I thought libc would depend on an existing operating system.</p>
<p>Another cross compilation question: </p>
<p>How can you tell, when compiling to a target other than the host the compiler is running on, that the native C library is being statically linked?</p>
<p>It's not clear how host independent cross compiling is with cargo - does the C library linked in a staticlib depend on the toolchain used to compile the crate?</p>
<p>Also:</p>
<p>From <a href="https://github.com/rust-lang/rust/blob/3c9442fc503fe397b8d3495d5a7f9e599ad63cf6/src/librustc_back/target/mod.rs#L70-L207">source code</a> and <a href="https://doc.rust-lang.org/reference/conditional-compilation.html">documentation</a> I can't tell what the <strong>target_vendor</strong> field for a custom target means in the context of conditional compilation, because vendor and OS are separate.</p>



<a name="234262935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/build-std%20%2B%20libc/near/234262935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/build-std.20.2B.20libc.html#234262935">(Apr 13 2021 at 03:29)</a>:</h4>
<p>I'm not sure I'm understanding the question correctly, but which libc is linked depends on how the custom target is defined. The exact particulars of which libraries are linked are defined in the <a href="https://github.com/rust-lang/libc">libc crate</a>. You have to follow the <code>cfg</code> expressions that match your target to the <code>#[link]</code> attributes. For targets without an OS, it doesn't link anything. For a Windows target, it'll link <code>msvcrt</code> or <code>libcmt</code>. What's linked on unix-like things is <a href="https://github.com/rust-lang/libc/blob/2c40088cced1fdbd3230d521ae5babb73fbe33a6/src/unix/mod.rs#L299-L387">this monstrosity</a>.</p>
<p>If you're asking how the linker finds the C libraries, it (mostly) works like any other library. Typically on linux distributions there will be packages available that include pre-built libc for various targets along with special linkers for handling those targets. You specify the target-specific linker in cargo's config and that linker will use the appropriate libc.</p>



<a name="234262939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/build-std%20%2B%20libc/near/234262939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/build-std.20.2B.20libc.html#234262939">(Apr 13 2021 at 03:29)</a>:</h4>
<blockquote>
<p>How can you tell, when compiling to a target other than the host the compiler is running on, that the native C library is being statically linked?</p>
</blockquote>
<p>The only way I know of is to run a command like <code>rustc +nightly --print=target-spec-json -Zunstable-options --target x86_64-unknown-linux-musl</code> and look for the <code>"crt-static-default"</code> field (which defaults to false if not shown).</p>



<a name="234263007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/build-std%20%2B%20libc/near/234263007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/build-std.20.2B.20libc.html#234263007">(Apr 13 2021 at 03:30)</a>:</h4>
<blockquote>
<p>It's not clear how host independent cross compiling is with cargo - does the C library linked in a staticlib depend on the toolchain used to compile the crate?</p>
</blockquote>
<p>It depends on the target and which linker you are using (and various other flags that you can pass in to change it).</p>



<a name="234263104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/build-std%20%2B%20libc/near/234263104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/build-std.20.2B.20libc.html#234263104">(Apr 13 2021 at 03:30)</a>:</h4>
<blockquote>
<p>From source code and documentation I can't tell what the target_vendor field for a custom target means in the context of conditional compilation, because vendor and OS are separate.</p>
</blockquote>
<p>The <code>target_vendor</code> field is usually "unknown" unless there is a reason for it to be something else. It is a somewhat historical inheritance from gcc. For example, there's a target that only differs between the vendor "sun" and "pc", but that difference is very rare.</p>



<a name="234263126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/build-std%20%2B%20libc/near/234263126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/build-std.20.2B.20libc.html#234263126">(Apr 13 2021 at 03:30)</a>:</h4>
<p>@ender ^</p>



<a name="234265432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/build-std%20%2B%20libc/near/234265432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ender <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/build-std.20.2B.20libc.html#234265432">(Apr 13 2021 at 04:08)</a>:</h4>
<p>This is super helpful!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>